﻿//------------------------------------------------------------
// Game Framework
// Copyright © 2013-2021 Jiang Yin. All rights reserved.
// Homepage: https://gameframework.cn/
// Feedback: mailto:ellan@gameframework.cn
//------------------------------------------------------------

using System;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.UI;
using UnityGameFramework.Runtime;

namespace Adventure
{
    public class LoginForm : UGuiForm
    {
        private InputField userInput;
        private InputField passwordInput;
        private UIButton loginButton;
        private UIButton registerButton;
        private UIButton settingButton;
        private UIButton aboutButton;

        protected override void OnInit(object userData)
        {
            base.OnInit(userData);

            userInput = transform.FindTransform<InputField>("UserInputField");
            passwordInput = transform.FindTransform<InputField>("PasswordInputField");
            registerButton = transform.FindTransform<UIButton>("RegisterButton");
            loginButton = transform.FindTransform<UIButton>("LoginButton");

            settingButton = transform.FindTransform<UIButton>("SettingButton");
            aboutButton = transform.FindTransform<UIButton>("AboutButton");
        }
        protected override void OnOpen(object userData)
        {
            base.OnOpen(userData);

            UIEventTrigger.Get(registerButton).onClick = OnRegisterButtonClick;
            UIEventTrigger.Get(loginButton).onClick = OnLoginButtonClick;
            UIEventTrigger.Get(settingButton).onClick = OnSettingButtonClick;
            UIEventTrigger.Get(aboutButton).onClick = OnAboutButtonClick;

            if (GameEntry.ArchiveData.Load(Constant.Setting.UserData))
            {
                userInput.text = GameEntry.ArchiveData.Data.player.User;
                passwordInput.text = GameEntry.ArchiveData.Data.player.Password;
            }
        }

        protected override void OnClose(bool isShutdown, object userData)
        {
            base.OnClose(isShutdown, userData);

            UIEventTrigger.Get(registerButton).onClick = null;
            UIEventTrigger.Get(loginButton).onClick = null;
            UIEventTrigger.Get(settingButton).onClick = null;
            UIEventTrigger.Get(aboutButton).onClick = null;
        }

        private void OnRegisterButtonClick(GameObject go)
        {
            PlayUISound(10000);
            GameEntry.UI.OpenUIForm(UIFormId.RegisterForm);
            Close();
        }
        private void OnLoginButtonClick(GameObject go)
        {
            PlayUISound(10000);

            if (GameEntry.UI.HasUIForm(UIFormId.TipForm)) return;

            GameEntry.ArchiveData.Load(userInput.text);

            var arg1 = GameEntry.ArchiveData.Data.player;

            //核对账号和密码是否一致
            if (arg1.User == userInput.text && arg1.Password == passwordInput.text)
            {
                GameEntry.Setting.SetString(Constant.Setting.UserData, GameEntry.ArchiveData.Data.player.User);

                string content = GameEntry.Localization.GetString("Tip.LoginSucceed");
                int colorCode = 2;
                GameEntry.UI.OpenUIForm(UIFormId.TipForm, Tuple.Create(content, colorCode));

                LoginSucceed();
            }
            else
            {
                string content = GameEntry.Localization.GetString("Tip.LoginErro");
                int colorCode = 1;
                GameEntry.UI.OpenUIForm(UIFormId.TipForm, Tuple.Create(content, colorCode));
            }

            GameEntry.TimerComponent.AddOnceTimer(1000, () => { GameEntry.UI.CloseUIForm(GameEntry.UI.GetUIForm(UIFormId.TipForm)); });
        }

        /// <summary>
        /// 登录成功
        /// </summary>
        private async void LoginSucceed()
        {
            await Task.Delay(1000);
            GameEntry.UI.OpenUIForm(UIFormId.MenuForm, GameEntry.Procedure.GetProcedure<ProcedureMenu>());
            Close();
        }

        public void OnSettingButtonClick(GameObject go)
        {
            Log.Info("设置");
            PlayUISound(10000);
            GameEntry.UI.OpenUIForm(UIFormId.SettingForm);
            Close();
        }

        public void OnAboutButtonClick(GameObject go)
        {
            Log.Info("关于");
            PlayUISound(10000);
            GameEntry.UI.OpenUIForm(UIFormId.AboutForm);
            Close();
        }
    }
}
